# -*- coding: utf-8 -*-
import bbs

from flask import Flask, request,jsonify

app = Flask(__name__)

class Process:
    def __init__(self,uid,authcode):
        self.authcode = authcode
        self.uid = uid
    def arg(self,arg):
        self.arg=arg
        return self
    def Package(self,event):
        ret = self.__Route(event)
        if ret == False:

        Package={
            "type":ret[0],
            "Data":ret[1],
            "code":ret[2],
            "Msg": ret[3]
        }
        return Package
    def __Route(self,event):
        route = {
            "TIPS_POST":self.__POST__
            "TIPS_LIST":self.__TIPS__,
            "ERROR_NOPID":self.__ERROR__
        }
        if event not in route:
            return ["error","",-1,"无此事件"]
        else:
            return route[event]()
    def __ERROR__(self,event):
        if event == "ERROR-NOPID":
            return ["error","",-2,"请输入PID"]
        if event == "ERROR-NOTITLE":
            return ["error", "", -3, "请输入标题"]
        if event == "ERROR-CONTENT":
            return ["error", "", -3, "请输入文本内容"]

    def __POST__(self):
        if "title" not in self.arg or "content" not in self.arg:
            return self.__ERROR__("ERROR-NOPID")
        else:

    def __TIPS__(self):
        TIPS_LIST = []
        if "page" not in self.arg:
            return self.__ERROR__("ERROR-NOPID")
        else:
            err,ret = bbs.post_list(self.uid,self.authcode,self.arg["page"])
        for category in ret:
            for i in ret[category] :
                TIPS={
                    "tid":i["pid"],
                    "title":i["title"],
                    "send_":i["author_id"],
                    "last_":i["last_reply_id"],
                    "reply_num":i["reply_count"],
                    "ctime":i["ts"],
                    "mtime":i["last_reply_ts"],
                    "is_stuck":1,
                    "is_marked":i["is_marked"]
                }
                TIPS_LIST.append(TIPS)
        return ["TIPS_LIST",TIPS_LIST,0,""]

@app.route("/api/post_list")
@app.route("/api/post_list/<int:page>")
def post_list(page):
    uid = request.cookies.get("uid")
    authcode = request.cookies.get("authcode")
    if not page:
        return jsonify(Process(uid,authcode).Package("TIPS_LIST"))
    else:
        return jsonify(Process(uid,authcode).arg({"page":page}).Package("TIPS_LIST"))
@app.route("/api/post/")
def post():
    uid = request.cookies.get("uid")
    authcode = request.cookies.get("authcode")

    title = request.form["title"]
    content = request.form["content"]
    if not title :
        return jsonify(Process(uid,authcode).Package("ERROR-NOTITLE"))
    if not content :
        return jsonify(Process(uid,authcode).Package("ERROR-NOCONTENT"))
    else:
        return jsonify(Process(uid,authcode).arg({"title":title,"content":content}).Package("TIPS_POST"))
@app.route("/api/post_/<int:pid>")
def post_(pid):
    uid = request.cookies.get("uid")
    authcode = request.cookies.get("authcode")
    if not pid:
        return jsonify(Process(authcode).Package("ERROR-NOPID"))
    else:
        content = request.form["content"]
